// 类目列表
controllers.controller('forestListCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'Forest', 'AlertService', 'uiGridConstants', function ($scope, $http, $rootScope, $modal, $window, Forest, AlertService, uiGridConstants) {

    $scope.resetCurrentPageName("类目维护");
    $scope.utils = Food.utils;
    $scope.pageSize = 10;
    $scope.currentPage = 1;
    $scope.forestList = Forest.forestList;
    $scope.forestTitle = "";
    $scope.forestParentId;

    $scope.queryList = function () {
        Forest.queryList($scope.currentPage, $scope.pageSize, $scope.forestTitle, $scope.forestParentId, function () {
            $scope.totalItems = Forest.totalSceneCount;
        });
    };
    $scope.queryList();
    $scope.changeForestDetail = function (forest) {
        Forest.currentForest = forest;
        $window.location.href = "#forest/forestDetail";
    };

    var operateTemplate = "<button class='btn btn-primary btn-sm' ng-click='grid.appScope.changeForestDetail(row.entity)'>类目详情</button>";
    $scope.gridOptions = {
        data: 'forestList',
        enableColumnMenus: false,
        enableColumnResizing: true,
        enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER,
        rowHeight: 50,
        columnDefs: [

            {field: 'parentId', displayName: '父节点Id'},
            {field: 'userId', displayName: '创建者Id'},
            {field: 'title', displayName: '类目标题'},
            {
                field: 'image',
                displayName: '图片',
                width: "10%",
                cellTemplate: '<div class="ui-grid-cell-contents"><img src="{{row.entity.image}}" style="width: 50px"/></div>'
            },
            {
                field: 'status',
                displayName: '状态',
                cellTemplate: '<div>{{row.entity.state == 0 ? "停用" :"在用"}}</div>'
            },
            {
                field: 'tariff',
                displayName: '关税'
            },
            {
                field: 'id',
                displayName: '操作',
                width: "30%",
                cellTemplate: operateTemplate
            }
        ]
    };
}])
;

// 类目详情
controllers.controller('forestDetailCtrl', ['$scope', '$http', '$rootScope', '$modal', 'Forest', 'AlertService', function ($scope, $http, $rootScope, $modal, Forest, AlertService) {
    $scope.resetCurrentPageName("类目维护");
    $scope.currentForest = Forest.currentForest;
    $scope.forestPropertyList = Forest.forestPropertyList;
    $scope.queryForestProperty = function () {
        Forest.queryForestProperty($scope.currentForest.id, $scope.pageSize, function () {
            $scope.totalItems = Forest.totalSceneCount;
        });
    };

    $scope.updateForest = function () {
        Forest.updateForest($scope.currentForest, function () {
            AlertService.addAlert({type: 'success', msg: '修改成功! '}, 1000);
        });
    };

    $scope.remove = function (forestPropertyId) {
        Forest.removeForestProperty(forestPropertyId, function () {

        });
    };
    $scope.queryForestProperty();

    $scope.addForestProperty = function () {
        //弹框选择属性，然后保存
        (function () {
            $rootScope.contextIsModal = true;
            $rootScope.modalIns = $modal.open({
                templateUrl: 'addForestProperty.html',
                animation: true,
                controller: 'propertyListSelectCtrl',
                windowClass: 'app-modal-window',
                size: 'sm',
                resolve: {}
            });
            $rootScope.ok = function () {
                //加入类目属性哦
                var propertyIds = "";
                for (var index in $rootScope.selectedItemList) {
                    propertyIds += $rootScope.selectedItemList[index].id + ","
                }
                Forest.insertForestPropertys($scope.currentForest.id, propertyIds, function () {
                    $rootScope.modalIns.dismiss();
                });
            };
            $rootScope.cancel = function () {
                $rootScope.modalIns.dismiss();
            };
            $rootScope.modalIns.result.then(function () {
                $rootScope.contextIsModal = false;

            }, function () {
                $rootScope.contextIsModal = false;
            });
        })();

    };
    var operateTemplate = "<button class='btn btn-primary btn-sm' ng-click='grid.appScope.remove(row.entity.id)'>删除该属性</button>";
    $scope.gridOptions = {
        data: 'forestPropertyList',
        enableColumnMenus: false,
        enableColumnResizing: true,
        rowHeight: 40,
        columnDefs: [

            {field: 'forestId', displayName: '类目Id'},
            {field: 'propertyId', displayName: '属性Id'},
            {field: 'propertyTitle', displayName: '属性标题'},
            {
                field: 'id',
                displayName: '操作',
                width: "10%",
                cellTemplate: operateTemplate
            }
        ]
    };

    $scope.addImage = function () {
        //弹框选择属性，然后保存
        (function () {
            $rootScope.contextIsModal = true;
            $rootScope.modalIns = $modal.open({
                templateUrl: './partials/image/imageList.html',
                animation: true,
                controller: 'imageListCtrl',
                windowClass: 'app-modal-window',
                size: 'lg',
                resolve: {}
            });
            $rootScope.ok = function () {
                //加入类目属性哦
                var picUrl = "";
                for (var index in $rootScope.selectedItemList) {
                    picUrl = $rootScope.selectedItemList[index].url
                }
                $scope.currentForest.image = picUrl;
                $rootScope.modalIns.dismiss();
            };
            $rootScope.cancel = function () {
                $rootScope.modalIns.dismiss();
            };
            $rootScope.modalIns.result.then(function () {
                $rootScope.contextIsModal = false;

            }, function () {
                $rootScope.contextIsModal = false;
            });
        })();

    };

}]);

// 新增类目
controllers.controller('addForestCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'Forest', 'AlertService', function ($scope, $http, $rootScope, $modal, $window, Forest, AlertService) {
    $scope.resetCurrentPageName("类目维护");
    $scope.forestTitle = "";
    $scope.forestParentId = "";
    $scope.tariff = "";
    $scope.rootForestList = Forest.rootForestList;
    Forest.getForestTree(function () {

    });

    $scope.addForest = function () {
        Forest.insert($scope.forestTitle, $scope.tariff, $scope.forestImage, $scope.forestParentId, function () {
            AlertService.addAlert({type: 'success', msg: '新增类目成功! '}, 1000);
            $window.location.href = "#forest/forestList";
        });
    };

    $scope.addImage = function () {
        //弹框选择属性，然后保存
        (function () {
            $rootScope.contextIsModal = true;
            $rootScope.modalIns = $modal.open({
                templateUrl: './partials/image/imageList.html',
                animation: true,
                controller: 'imageListCtrl',
                windowClass: 'app-modal-window',
                size: 'lg',
                resolve: {}
            });
            $rootScope.ok = function () {
                //加入类目属性哦
                var picUrl = "";
                for (var index in $rootScope.selectedItemList) {
                    picUrl += $rootScope.selectedItemList[index].url
                }
                $scope.forestImage = picUrl;
                $rootScope.modalIns.dismiss();
            };
            $rootScope.cancel = function () {
                $rootScope.modalIns.dismiss();
            };
            $rootScope.modalIns.result.then(function () {
                $rootScope.contextIsModal = false;

            }, function () {
                $rootScope.contextIsModal = false;
            });
        })();

    };

}]);

